#include "headers/queue.h"
#include "../memory/headers/kheap.h"


queue_type* create_queue(u32int size)
{
	queue_type* queue = kmalloc(sizeof(queue_type));
	queue->array = kmalloc(size);
	queue->head = queue->tail = queue->count =  0;
	queue->size = size;
	
	return queue;
}

void destroy_queue(queue_type* queue)
{
	kfree(queue->array);
	kfree(queue);
}

void enqueue(u8int element, queue_type* queue)
{
    queue->array[queue->tail++] = element;
	if(queue->tail >= queue->size)
		queue->tail = 0;
	if(queue->count != queue->size)
	{
		queue->count++;
	}
	else
	{
		queue->head++;
		if(queue->head >= queue->size)
			queue->head = 0;
	}
}

u8int dequeue(queue_type* queue)
{
	u8int temp;
	if(queue->count == 0)
		return QUEUE_ERROR;
	
	temp = queue->array[queue->head++];
	queue->count--;
	if(queue->head >= queue->size)
			queue->head = 0;
	return temp;
}

BOOL queue_is_empty(queue_type* queue)
{
	return queue->count?false:true;
}

u32int queue_get_element_n(queue_type* queue)
{
	return queue->count;
}

void clear_queue(queue_type* queue)
{
	queue->head = queue->tail = queue->count =  0;
}